#ifndef __CAMERA_H__
#define __CAMERA_H__

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace std::chrono;

cv::Mat cameraMatrix;
cv::Mat distCoeffs;

vector<int> detectCamera(int tryMax = 4) {
  vector<int> cameraList;
  for (int i = 0; i < tryMax; i++) {
    cv::VideoCapture cap(i);
    if (cap.isOpened()) {
      cameraList.push_back(i);
      cap.release();
    }
  }
  return cameraList;
}

void initCamera(cv::VideoCapture &cap, int tryMax = 10, bool value = 0) {
  auto availableCam = detectCamera();
  for (auto index : availableCam) {
    cout << "Camera " << index << " is available." << endl;
  }
  int maxIndex = *max_element(availableCam.begin(), availableCam.end());
  int minIndex = *min_element(availableCam.begin(), availableCam.end());
  int index = value ? maxIndex : minIndex;

  cap = cv::VideoCapture(index, cv::CAP_V4L2);
  if (!cap.isOpened()) {
    cerr << "无法打开摄像头！" << endl;
  }
  cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
  cap.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
  cap.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
}

#endif //__CAMERA_H__